package com.atcx.controller;


import com.atcx.pojo.User;
import com.atcx.service.UserService;
import com.atcx.util.PageResult;
import com.atcx.util.QueryPageBean;
import com.atcx.util.Result;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author pangShu
 * @since 2022-03-23
 */
@RestController
@RequestMapping("/user")
public class UserController {



    private Result result;
    @Resource
    private UserService userService;
    /**
     * @Author:pangshu
     * @Description: 账户注册，
     * @Date:20:55 2022/3/23
     * @Param:有名字，性别，年龄，用户名，密码
     * @return: com.atcx.util.Result
     **/
    @RequestMapping("getNameExist")
    public Result getNameExist(String name){
        boolean exist = userService.getNameExist(name);
        if (exist){
            result = new Result(true,"名字可以注册");
        }else {
            result = new Result(false,"名字已经存在，请从新填写");
        }
        return result;
    }
    @RequestMapping("usernameExist")
    public Result usernameExist(String username){
        Boolean exist = userService.getUserNameExist(username);
        if (exist){
            result = new Result(true,"账户可以注册");
        }else {
            result = new Result(false,"账户已经存在，请从新填写");
        }
        return result;
    }
    @RequestMapping("regiest")
    public Result regiest(@RequestBody User user ,HttpServletResponse response){

        int i = userService.addUser(user);
        if (i>0){
            result = new Result(true,"注册成功");
        }else {
            result = new Result(false,"注册信息失败");
        }
        return result;
    }

    /**
     * @Author:pangshu
     * @Description: 账户密码登录，输入正确跳转不同页面
     * @Date:21:22 2022/3/23
     * @Param:
     * @return: void
     **/
    @RequestMapping("adminLogin")
    public void login(String username ,String password,HttpServletResponse response){
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        User userByNP = userService.getUserByNP(user);
        System.out.println(userByNP);
        System.out.println("管理员登录");
            if (userByNP!=null) {
                //账户密码正确
                System.out.println("密码正确");
                Cookie cookie = new Cookie("userId", userByNP.getUserid().toString());
                cookie.setMaxAge(60 * 60);//单位是秒
                cookie.setPath("/");//路径
                response.addCookie(cookie);
                    //管理员登录
                    System.out.println("跳转开始");
                    if ("admin".equals(userByNP.getUsername())&&"A".equals(userByNP.getLevel())){
                        try {
                            response.sendRedirect("../pages/teacherManager.html");
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }else {
                        if ("C".equals(userByNP.getLevel())){
                            try {
                                //教师登录
                                response.sendRedirect("../pages/employeeManager.html");
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }else {
                            try {
                                response.sendRedirect("../login/index.html");
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }

                    }



//                } else if ("B".equals(user.getLevel())) {
//                    //学生登录
//                    System.out.println("跳转开始");
//                    response.sendRedirect("../pages/studentManager.html");
//
//
//                }


            }else {
                try {
                    response.sendRedirect("../login/index.html");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }



    }
    @RequestMapping("userLogin")
    public void userLogin(String username ,String password,HttpServletResponse response){
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        User userByNP = userService.getUserByNP(user);
        System.out.println(userByNP);
        System.out.println("学生登录");
        if (userByNP!=null) {
            //账户密码正确
            System.out.println("密码正确");
            Cookie cookie = new Cookie("userId", userByNP.getUserid().toString());
            cookie.setMaxAge(60 * 60);//单位是秒
            cookie.setPath("/");//路径
            response.addCookie(cookie);
            //学生登录
            if ("B".equals(userByNP.getLevel())){
                try {
                    response.sendRedirect("../pages/studentManager.html");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }else {
                try {
                    response.sendRedirect("../login/index.html");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }else {
            try {
                response.sendRedirect("../login/index.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    /**
     * @Author:pangshu
     * @Description: 修改用户信息
     * @Date:21:28 2022/3/23
     * @Param:
     * @return: com.atcx.util.Result
     **/
    @RequestMapping("update")
    public Result update(@RequestBody User user){
        int i = userService.updateUser(user);
        if (i>0){
            result = new Result(true,"修改用户信息成功");
        }else {
            result = new Result(false,"修改用户信息失败");
        }
        return result;
    }

    @RequestMapping("deleteUser")
    public Result deleteUser(@Param("id") int id){
        int i = userService.deleteUser(id);
        if (i>0){
            result = new Result(true,"删除用户成功");
        }else {
            result = new Result(false,"删除用户失败,请先删除关联信息");
        }
        return result;
    }


    @RequestMapping("findByPage")
    public Result findByPage(@RequestBody QueryPageBean queryPageBean){
        PageResult page = userService.findByPage(queryPageBean);
        if (page != null){
            result = new Result(true,"查询用户信息成功",page);
        }else {
            result = new Result(false,"查询用户信息失败");
        }
        return result;
    }
    @RequestMapping("getUserId")
    public Result getUserId(HttpServletRequest request){
//        Integer UserId = 0;
//        Cookie[] cookies = request.getCookies();
//        for (Cookie cookie : cookies) {
//            if (cookie.getName().equals("userId")){
//                String value = cookie.getValue();
//                UserId = Integer.parseInt(value);
//            }
//        }
        Integer UserId = getUId(request);
        User user = null;
        if (UserId>0){
            user = userService.getUserById(UserId);
        }
        if (user!=null){
            result = new Result(true,"查询当前用户成功",user);
        }else {
            result = new Result(false,"查询用户信息失败");
        }
        return result;
    }
    //退出
    @RequestMapping("exit")
    public void exit(HttpServletResponse response,HttpServletRequest request){
        //先删除cookie里边数据
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            cookie = new Cookie(cookie.getName(), null);
            cookie.setMaxAge(0);
            response.addCookie(cookie);
        }
    }
    //从后台添加用户
    @RequestMapping("addUser")
    public Result addUser(@RequestBody User user){
        int i = userService.addUser(user);
        if (i>0){
            result = new Result(true,"添加用户信息成功");
        }else {
            result = new Result(false,"添加用户信息失败");
        }
        return result;
    }
    public  static Integer getUId(HttpServletRequest request){
        Integer UserId = 0;
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals("userId")){
                String value = cookie.getValue();
                UserId = Integer.parseInt(value);
                break;
            }
        }
        return UserId;
    }
}

